/*
 * Copyright (c) 2010-2011 Plugger Antonio Begue Ponce. All Rights Reserved.
 *
 * This file is part of Plugger Framework.
 *
 *  Plugger Framework is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License version 3 as published by
 *  the Free Software Foundation.
 *
 *  Plugger Framework is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with Plugger Framework.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.plugger.util;

import java.math.BigDecimal;

/**
 * A static helper for math operations.
 *
 * @author Antonio Begue
 * @version $Revision: 1.0 $
 */
public class MathHelper {
    private static int decimals = 2;

    /**
     * Return a rounded number to a specified decimal places
     * @return A double number.
     * @param d the double number to round
     * @param decimalPlace the decimal places to round to
     */
    public static double round(double d, int decimalPlace) {
        // see the Javadoc about why we use a String in the constructor
        // http://java.sun.com/j2se/1.5.0/docs/api/java/math/BigDecimal.html#BigDecimal(double)
        BigDecimal bd = new BigDecimal(Double.toString(d));
        bd = bd.setScale(decimalPlace, BigDecimal.ROUND_HALF_UP);
        return bd.doubleValue();
    }

    /**
     * Return a rounded number to the current specified decimal places
     * @return A double number.
     * @param d the double number to round
     */
    public static double round(double d) {
        return round(d, decimals);
    }

    /**
     * Return the value part of a string.
     * @param str The string to evaluate.
     * @return A double number.
     */
    public static double val(String str) { // TODO: Revisar
        StringBuilder validStr = new StringBuilder();
        boolean seenDot = false;   // when this is true, dots are not allowed
        boolean seenDigit = false; // when this is true, signs are not allowed
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            if (c == '.' && !seenDot) {
                seenDot = true;
                validStr.append(c);
            } else if ((c == '-' || c == '+') && !seenDigit) {
                validStr.append(c);
            } else if (Character.isDigit(c)) {
                seenDigit = true;
                validStr.append(c);
            } else if (Character.isWhitespace(c)) {
                // just skip over whitespace
                continue;
            } else {
                // invalid character
                break;
            }
        }
        return Double.parseDouble(validStr.toString());
    }

    /**
     * Return the current decimal places.
     * @return the decimal places
     */
    public static int getDecimales() {
        return decimals;
    }

    /**
     * Change the current decimal places.<BR/>
     * Default is 2.
     * @param value the decimals to set
     */
    public static void setDecimales(int value) {
        decimals = value;
    }
}
